package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.UserBank;
import com.chen.dto.R;
import com.chen.dto.UserBankDto;
import com.chen.fegin.UserBankFegin;
import com.chen.service.UserBankService;
import com.chen.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Date: 2021/4/26 10:54 AM
 * @Version 1.0
 */
@RestController
@Api(tags = "用户银行卡接口控制器")
@RequestMapping("/userBanks")
public class UserBankController implements UserBankFegin {

    @Autowired
    private UserBankService userBankService;


    @GetMapping
    @ApiOperation("根据用户id查询用户银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户的Id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<Page<UserBank>> list(Page<UserBank> page, String usrId) {

        QueryWrapper<UserBank> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(usrId)) {
            queryWrapper.eq("user_id", usrId);
        }
        queryWrapper.orderByDesc("last_update_time");

        return R.ok(userBankService.page(page, queryWrapper));

    }


    @PatchMapping
    @ApiOperation("修改用户银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "银行卡的Id"),
            @ApiImplicitParam(name = "status", value = "银行卡的状态"),
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<Page<UserBank>> update(@ApiIgnore @RequestBody UserBank userBank) {
        userBankService.updateById(userBank);
        return R.ok();

    }


    @PostMapping("/status")
    @ApiOperation(value = "启用/禁用用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id"),
            @ApiImplicitParam(name = "status", value = "状态码")
    })
    @PreAuthorize("hasAuthority('user_update')")
    public R<Object> status(@RequestParam("id") String userId,
                            @RequestParam("status") Byte status) {
        UserBank user = new UserBank();
        user.setId(Long.parseLong(userId));
        user.setStatus(status);
        userBankService.updateById(user);
        return R.ok();

    }

    @GetMapping("/current")
    public R<UserBank> info() {
        QueryWrapper<UserBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", UserUtil.getUser());
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("last_update_time");
        List<UserBank> list = userBankService.list(queryWrapper);
        return R.ok(CollectionUtils.isEmpty(list) ? null : list.get(0));
    }

    @PostMapping("/bind")
    public R<Object> bind(@RequestBody UserBank userBank) {
        userBankService.bind(userBank);
        return R.ok();
    }


    @Override
    public UserBankDto getUserBank(List<Long> ids) {
        QueryWrapper<UserBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",ids);
        queryWrapper.eq("status",1);
        List<UserBank> list = userBankService.list(queryWrapper);
        List<UserBankDto> transform = list.stream().map(userBank -> {
            UserBankDto userBankDto = new UserBankDto();
            BeanUtils.copyProperties(userBank, userBankDto);
            return userBankDto;
        }).collect(Collectors.toList());
        return transform.get(0);
    }
}
